<?php

namespace app\webapi\controller;

use think\facade\Cache;

class ApiRedis extends ApiBaseController
{
    /**
     * 设置用户成功抢购的数量
     * @param {*} uid
     */
    public static function setUserRushSuccess($uid, $sid, $timeout, $count = 1)
    {
        if (APiRedisServer::selected(2)->exists("success_{$uid}_{$sid}_count")) {
            APiRedisServer::selected(2)->INCRBY("success_{$uid}_{$sid}_count", $count);
        } else {
            APiRedisServer::selected(2)->setex("success_{$uid}_{$sid}_count", $timeout, $count);
        }
    }

    /** 获取用户成功抢购的数量 */
    public static function getUserRushSuccess($uid, $sid)
    {
        return APiRedisServer::selected(2)->get("success_{$uid}_{$sid}_count");
    }


    /**
     * 更新获取
     * @param {*} key
     * @param {*} callback
     */
    public static function updateGet($key, $func)
    {
        $data = APiRedisServer::selected(0)->get($key);
        if (empty($data)) {
            $data = $func();
            APiRedisServer::selected(0)->set($key, json_encode($data));
        } else {
            $data = json_decode($data, true);
        }
        return $data;
    }

    public static function updateGetByDB($db,$key, $func)
    {
        $data = APiRedisServer::selected($db)->get($key);
        if (empty($data)) {
            $data = $func();
            APiRedisServer::selected($db)->set($key, json_encode($data));
        } else {
            $data = json_decode($data, true);
        }
        return $data;
    }
    public static function getUserGodState($uid)
    {
        $limit = APiRedisServer::selected(2)->get(self::KEYS['USER_LIMIT'] . $uid);
        $limit_remain = APiRedisServer::selected(2)->ttl(self::KEYS['USER_LIMIT'] . $uid);
        $blacklist = APiRedisServer::selected(2)->get(self::KEYS['USER_BLACKLIST'] . $uid);
        $blacklist_remain = APiRedisServer::selected(2)->ttl(self::KEYS['USER_BLACKLIST'] . $uid);
        $blackroom = APiRedisServer::selected(2)->get(self::KEYS['USER_BLACKROOM'] . $uid);
        $blackroom_remain = APiRedisServer::selected(2)->ttl(self::KEYS['USER_BLACKROOM'] . $uid);
        $godtime = APiRedisServer::selected(2)->get(self::KEYS['USER_GODTIME'] . $uid);
        $godtime_remain = APiRedisServer::selected(2)->ttl(self::KEYS['USER_GODTIME'] . $uid);
        $datalimit = [
            'use' => !empty($limit) && $limit > 0 ? true : false,
            'data' => $limit > 0 ? $limit : 0,
            'timeout' => $limit_remain > 0 ? $limit_remain : 0
        ];
        $datablacklist = [
            'use' => $blacklist ? true : false,
            'data' => $blacklist ? json_decode($blacklist, true) : [],
            'timeout' => $blacklist_remain > 0 ? $blacklist_remain : 0
        ];
        $datablackroom = [
            'use' => $blackroom > 0 ? true : false,
            'data' => $blackroom > 0 ? $blackroom : 0,
            'timeout' => $blackroom_remain > 0 ? $blackroom_remain : 0
        ];
        $datagodtime = [
            'use' => $godtime > 0 ? true : false,
            'data' => $godtime > 0 ? $godtime : 0,
            'timeout' => $godtime_remain > 0 ? $godtime_remain : 0
        ];

        $result = [
            'limit' => $datalimit,
            'blacklist' => $datablacklist,
            'blackroom' => $datablackroom,
            'godtime' => $datagodtime
        ];

        return $result;

    }


}